package com.example.mypro;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class SchoolView_ItemActivity extends Activity {
	private ViewFlipper viewFlipper;
	private Intent intent;
	private GestureDetector detector;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_showview);
		viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
		intent = getIntent();
		Bundle bundle = intent.getBundleExtra("bundle");
		int[] imageIds = bundle.getIntArray("imageIds");
		int imageId = bundle.getInt("imageId");
		detector = new GestureDetector(SchoolView_ItemActivity.this, listener);
		for (int i = imageId; i < imageIds.length; i++) {
			viewFlipper.addView(addView(imageIds[i]));
		}
		for (int i = 0; i < imageId; i++) {
			viewFlipper.addView(addView(imageIds[i]));
		}
	}

	public View addView(int id) {
		ImageView imageView = new ImageView(this);
		imageView.setImageResource(id);
		return imageView;
	}

	private OnGestureListener listener = new OnGestureListener() {

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			return false;
		}

		@Override
		public void onShowPress(MotionEvent e) {

		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			return false;
		}

		@Override
		public void onLongPress(MotionEvent e) {

		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			if ((e2.getX() - e1.getX()) > 50) {
				viewFlipper.setInAnimation(AnimationUtils.loadAnimation(
						SchoolView_ItemActivity.this, R.anim.push_right_in));
				viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(
						SchoolView_ItemActivity.this, R.anim.push_right_out));
				viewFlipper.showNext();
				return true;

			} else if ((e2.getX() - e1.getX()) < -50) {
				viewFlipper.setInAnimation(AnimationUtils.loadAnimation(
						SchoolView_ItemActivity.this, R.anim.push_left_in));
				viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(
						SchoolView_ItemActivity.this, R.anim.push_left_out));
				viewFlipper.showPrevious();
				return true;
			}
			return false;
		}

		@Override
		public boolean onDown(MotionEvent e) {
			return false;
		}

	};

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return detector.onTouchEvent(event);
	}

	@Override
	protected void onPause() {
		super.onPause();
		finish();
	}

}
